package com.example.springbott.wx_service;


import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.MarketUserExample;
import com.example.springbott.bean.silic.WXAuthRegisterBo;
import com.example.springbott.mapper.MarketUserMapper;
import com.example.springbott.util.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 1784549954@qq.com
 * @since 2022/11/22 14:18
 */
@Service
public class WXAuthServiceImpl implements WXAuthService{

    @Autowired
    MarketUserMapper userMapper;

    /**
     * 注册 /register
     * 插入表中
     *
     * @param {"username":"woliale","password":"123456","mobile":"18029226881","
     * code":"073335","wxCode":"the code is a mock one"} 要验证code是否正确,检查手机号是否重复
     *
     * @return token: "eyJhbrza5IoWok"
     *          userInfo: {nickName: "woliale",…}
     *      data:{}
     *      errmsg: "成功"
     *      errno: 0
     * @author  silic xia
     * @since 2022/11/22 20:43
     */
    @Override
    public Integer wxAuthRegister(WXAuthRegisterBo wxAuthRegisterBo) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(wxAuthRegisterBo.getMobile());
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        if (marketUsers == null){return Count.REGISTER_MOBILE_REPEATED; }

        MarketUser user = new MarketUser();
        user.setAddTime(new Date());
        user.setUpdateTime(new Date());
        user.setStatus((byte)0);
        user.setDeleted(false);
        user.setMobile(wxAuthRegisterBo.getMobile());
        user.setUsername(wxAuthRegisterBo.getUsername());
        user.setNickname(wxAuthRegisterBo.getUsername());
        user.setPassword(wxAuthRegisterBo.getPassword());
        userMapper.insertSelective(user);
        return Count.SUCCESS_CODE;
    }

    /**
     *  重置密码 reset
     *
     * @param {mobile: "18022255555", code: "1234", password: "123456"}
     * @return {"errno":703,"errmsg":"验证码错误"}
     * @author  silic xia
     * @since 2022/11/23 9:31
     */
    @Override
    public void wxAuthReset(WXAuthRegisterBo wxAuthRegisterBo) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(wxAuthRegisterBo.getMobile());
        MarketUser user = new MarketUser();
        user.setPassword(wxAuthRegisterBo.getPassword());
        userMapper.updateByExampleSelective(user,example);
    }
}
